from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.contrib.auth.models import User
from django.utils.html import format_html
from django.urls import reverse
from bands.models import Musician, Band, UserProfile, Venue, Room
from datetime import datetime, date

class DecadeListFilter(admin.SimpleListFilter):
    title = 'Decade Born'
    parameter_name = 'decade'
    def lookups(self, request, model_admin):
        result = []
        this_year = datetime.now().year
        this_decade = (this_year // 10) * 10
        start = this_decade - 10
        for year in range(start, start - 100, -10):
            result.append((str(year), '%s - %s' % (year, year + 9)))
        return result

    def queryset(self, request, queryset):
        start = self.value()
        if start is None:
            return queryset
        start = int(start)
        return queryset.filter(birth__gte=date(start, 1, 1), birth__lte=date(start + 9, 12, 31))

@admin.register(Musician)
class MusicianAdmin(admin.ModelAdmin):
    list_display = ('id', 'last_name', 'birth', 'show_weekday', 'show_bands')
    search_fields = ('last_name', 'first_name')
    list_filter = (DecadeListFilter,)

    def show_weekday(self, obj):
        return obj.birth.strftime('%A')
    show_weekday.short_description = 'Birth Weekday'

    def show_bands(self, obj):
        bands = obj.band_set.all()
        if len(bands) == 0:
            return format_html('<i>None</i>')
        plural = 's' if len(bands) > 1 else ''
        params = '?id__in=' + ','.join([str(b.id) for b in bands])
        url = reverse("admin:bands_band_changelist") + params
        return format_html('<a href="{}">{} band{}</a>', url, len(bands), plural)
    show_bands.short_description = 'Bands'

@admin.register(Venue)
class VenueAdmin(admin.ModelAdmin):
    list_display = ("id", "name", "show_rooms")
    search_fields = ("name",)

    def show_rooms(self, obj):
        rooms = obj.room_set.all()
        if len(rooms) == 0:
            return format_html("<i>None</i>")

        plural = ""
        if len(rooms) > 1:
            plural = "s"

        parm = "?id__in=" + ",".join([str(b.id) for b in rooms])
        url = reverse("admin:bands_room_changelist") + parm
        return format_html('<a href="{}">Room{}</a>', url, plural)

    show_rooms.short_description = "Rooms"

@admin.register(Room)
class RoomAdmin(admin.ModelAdmin):
    list_display = (
        "id",
        "name",
    )
    search_fields = ("name",)

@admin.register(Band)
class BandAdmin(admin.ModelAdmin):
    pass

class UserProfileInline(admin.StackedInline):
    model = UserProfile
    can_delete = False

class UserAdmin(BaseUserAdmin):
    inlines = [UserProfileInline]

admin.site.unregister(User)
admin.site.register(User, UserAdmin)